package com.yixin.ystartlibrary.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.yixin.ystartlibrary.db.preference.GeneralPreferences;


/**
 * User: YStar
 * Date: 2018/1/28
 * Content 轻数据缓存:
 */
public class GeneralPreferencesUtils {

    /**
     * 获取String类型的储存信息从SharedPreference.
     *
     * @param context      From application.
     * @param key          在 {@link } 统一找到.
     * @param defaultValue It's String.
     * @return String
     */
    public static String getSharedPreference(Context context, String key, String defaultValue) {
        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
        return prefs.getString(key, defaultValue);
    }

    /**
     * 获取Integer类型的储存信息从SharedPreference.
     *
     * @param key          在 {@link } 统一找到.
     * @param context      From application.
     * @param defaultValue It's int.
     * @return int
     */
    public static int getSharedPreference(Context context, String key, int defaultValue) {
        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
        return prefs.getInt(key, defaultValue);
    }

    /**
     * 获取Boolean类型的储存信息从SharedPreference.
     *
     * @param key          在 {@link } 统一找到.
     * @param context      From application.
     * @param defaultValue It's boolean.
     * @return boolean
     */
    public static boolean getSharedPreference(Context context, String key, boolean defaultValue) {
        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
        return prefs.getBoolean(key, defaultValue);
    }

    /**
     * 获取Long类型的储存信息从SharedPreference.
     *
     * @param key          在 {@link } 统一找到.
     * @param context      From application.
     * @param defaultValue It's long.
     * @return long
     */
    public static long getSharedPreference(Context context, String key, long defaultValue) {
        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
        return prefs.getLong(key, defaultValue);
    }
    public static float getSharedPreference(Context context, String key, float defaultValue) {
        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
        return prefs.getFloat(key, defaultValue);
    }

    /**
     * 添加String类型的储存信息到SharedPreference.
     *
     * @param key     在 {@link } 统一设置.
     * @param context From application.
     * @param value   It's String.
     */
    public static void setSharedPreference(Context context, String key, String value) {
        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
        prefs.edit().putString(key, value).apply();
    }

    /**
     * 添加Boolean类型的储存信息到SharedPreference.
     *
     * @param key     在 {@link } 统一设置.
     * @param context From application.
     * @param value   It's boolean.
     */
    public static void setSharedPreference(Context context, String key, boolean value) {
        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }
    public static void setSharedPreference(Context context, String key, double value) {
        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putFloat(key, (float) value);
        editor.apply();
    }

    /**
     * 添加Integer类型的储存信息到SharedPreference.
     *
     * @param key     在 {@link } 统一设置.
     * @param context From application.
     * @param value   It's int.
     */
    public static void setSharedPreference(Context context, String key, int value) {
        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    /**
     * 添加Long类型的储存信息到SharedPreference.
     *
     * @param key     在 {@link } 统一设置.
     * @param context From application.
     * @param value   It's long.
     */
    public static void setSharedPreference(Context context, String key, long value) {
        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putLong(key, value);
        editor.apply();
    }
}
